<?php
    /**
     * 商品类别
     * Created by PhpStorm.
     * User: huangzhongwei
     * Date: 2018/10/24
     * Time: 9:09
     */

    namespace app\api\controller;

    use app\api\validate\GoodsType as GoodsTypeValidate;
    use app\api\validate\PagingParameter;
    use app\common\model\GoodsType as GoodsTypeModel;
    use app\api\validate\IDMustBePositiveInt;
    use app\common\model\University;
    use app\lib\exception\MissException;
    use app\lib\exception\SuccessMessage;
    use app\api\service\GoodsType as GoodsTypeService;

    class GoodsType extends BaseController
    {
        protected $beforeActionList = [
            'checkExclusiveScope' => ['only' => 'getallgoodstypebyuniversityid'],
            'checkSuperScope'     => ['only' => 'creategoodstype'],
        ];

        /**
         * 创建商品类型
         * @return SuccessMessage
         * @throws MissException
         * @throws \app\lib\exception\ParameterException
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function createGoodsType()
        {
            (new GoodsTypeValidate())->goCheck();
            $data = input('post.');
            //判断当前大学是否存在
            University::isExisted($data['university_id']);


            (new GoodsTypeModel())->save($data);
            return new SuccessMessage();
        }

        /**
         * 商品编辑
         * @return SuccessMessage
         * @throws MissException
         * @throws \app\lib\exception\ParameterException
         * @throws \think\Exception\DbException
         */
        public function edit()
        {
            (new GoodsTypeValidate())->goCheck();
            $data = input('post.');
            //判断当前大学是否存在
            University::isExisted($data['university_id']);
            $goodsType = (new GoodsTypeModel())->get($data['id']);
            if (!$goodsType) {
                throw new MissException(['msg' => '商品类别不存在', 'errorCode' => 80000]);
            }
            $goodsType->save($data);
            return new SuccessMessage();
        }

        /**
         * 获取指定大学下的商品类别包含商品
         *
         * @param $id 大学id
         *
         * @return array|\PDOStatement|string|\think\Collection
         * @throws MissException
         * @throws \app\lib\exception\ParameterException
         */
        public function getAllGoodsTypeByUniversityId($id)
        {
            (new IDMustBePositiveInt())->goCheck();
            //判断当前大学是否存在
            University::isExisted($id);
            $result = GoodsTypeModel::getAllGoodsTypeByUniversityId($id);
            return $result;
        }


        /**
         *
         * 获取所有商品类型
         *
         * @param int $page
         * @param int $size
         *
         * @return array
         * @throws \think\exception\DbException
         */
        public function getAllType($page = 1, $size = 30)
        {
            (new PagingParameter())->goCheck();
            $users = (new  GoodsTypeModel())->with('university')->order('create_time', 'desc')->paginate(
                $size, false, [
                'page' => $page,
            ]);

            if ($users->isEmpty()) {
                // 对于分页最好不要抛出MissException，客户端并不好处理
                return [
                    'current_page' => $users->currentPage(),
                    'data'         => [],
                ];
            }
            $data = $users->toArray();
            return [
                'current_page' => $users->currentPage(),
                'data'         => $data,
            ];
        }

        /**
         * 获取所有大学下的商品
         * @return array|\PDOStatement|string|\think\Collection|void
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function getUniversityWithType()
        {
            $university = (new University())->with('goodsType')->select();
            $cascader=(new GoodsTypeService())->toCascader($university);
//            dd($cascader);
            return $cascader;
        }


    }